package com.ruihang.generalibrary.utils;

import android.text.TextUtils;
import android.util.Log;

import com.jiongbull.jlog.JLog;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Administrator on 2016/12/17.
 */

public class DateUtils {

    private static long currentOnlyDateMil = -1;
    private static Date currentOnlyDate;
    private static long nextOnlyDate = -1;
    private static long preOnlyDate = -1;

    public static final String PATTERN_IN = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_IN_NEW = "MM-dd HH:mm";
    public static final String PATTERN_IN_OLD = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_OUT_TIME = "HH:mm";
    public static final String PATTERN_OUT_MONTH_TIME = "MM-dd HH:mm";
    public static final String PATTERN_BEFORE_SIX = "yyyyMMddHHmmss";
    public static final long OUTHOUR = -2880005L;//中国的时区早晨8点为0,晚上0点为-2880000
    public static final long sixBefore = 6 * 60 * 60 * 1000;
    public static final long sevenBefore = 7 * 60 * 60 * 1000;

    public static Date getDataDate(IDataDate dataDate) {
        if (dataDate == null || TextUtils.isEmpty(dataDate.getDateString())) {
            return null;
        }
        Date date = dataDate.getDataDate();
        if (date == null) {
            SimpleDateFormat sdf = new SimpleDateFormat(dataDate.getDatePattern());
            try {
                java.util.Date da = sdf.parse(dataDate.getDateString());
                if (da != null) {
                    date = new Date(da.getTime());
                }
                if (date != null) {
                    dataDate.setDataDate(date);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static String getStringDate(Date dataDate, String format) {
        if (dataDate == null || TextUtils.isEmpty(dataDate.toString())) {
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(dataDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date getDataDate(IDataMultiDate dataDate, int mode) {
        if (dataDate == null) {
            return null;
        }
        return getDataDate(dataDate.getDataDateInterface(mode));
    }

    public static Date getDataDateByDayYear(IDataDate dataDate) {
        if (dataDate == null || TextUtils.isEmpty(dataDate.getDateString())) {
            return null;
        }
        Date date = dataDate.getDataDate();
        if (date == null) {
            try {
                date = Date.valueOf(dataDate.getDateString());
                if (date != null) {
                    dataDate.setDataDate(date);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static long getCurentHoursLong() {
        Calendar c = Calendar.getInstance();
        String str = formatDate(c.getTime(), "HH:mm:ss");

//        c.set(Calendar.DAY_OF_WEEK,0);
//        c.set(Calendar.DAY_OF_WEEK_IN_MONTH,0);
//        c.set(Calendar.DAY_OF_YEAR,0);
        return getHoursLong(str);
    }

    public static long getHourLong(long holeMil) {
        if (holeMil <= 0) {
            return -1;
        }
        String str = formatDate(new Date(holeMil), "HH:mm:ss");
        return getHoursLong(str);
    }

    public static long getHoursLong(String hours) {
        if (TextUtils.isEmpty(hours)) {
            return -1;
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//        DateFormat sdf = DateFormat.getDateInstance();
        DateFormat sdf = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);
        try {
            java.util.Date date = sdf.parse(hours);
            if (date != null) {
                return date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static String getDateHourString(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            return sdf.format(date);
        }
        return null;
    }

    public static String getDateDayString(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            return sdf.format(date);
        }
        return null;
    }

    public static String getDateFullString(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
            return sdf.format(date);
        }
        return null;
    }

    public static int compareCurrentDate(String date) {
        if (TextUtils.isEmpty(date) || !date.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
            return -2;
        }
        Calendar c = Calendar.getInstance();
        return -2;
    }

    public static boolean isCurrentDate(String date) {
        try {
            if (!TextUtils.isEmpty(date)) {
                return isCurrentDate(Date.valueOf(date));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean isCurrentDate(Date date) {
        if (date == null) {
            return false;
        }

        if (currentOnlyDateMil == -1 || System.currentTimeMillis() >= nextOnlyDate) {
            initOnlyDate();
        }
        return isCurrentDate(date.getTime());
    }

    public static boolean isCurrentDate(long dateMil) {
        if (dateMil <= 0) {
            return false;
        }

        if (currentOnlyDateMil == -1 || System.currentTimeMillis() >= nextOnlyDate) {
            initOnlyDate();
        }

        if (dateMil == currentOnlyDateMil) {
            return true;
        } else {
            return dateMil > currentOnlyDateMil && dateMil < nextOnlyDate;
        }
    }

    public static boolean isYesterDayDate(Date date) {
        if (date == null) {
            return false;
        }
        return isCurrentDate(date.getTime());
    }

    public static boolean isYesterDayDate(long dateMil) {
        if (dateMil <= 0) {
            return false;
        }

        if (currentOnlyDateMil == -1 || System.currentTimeMillis() <= preOnlyDate) {
            initOnlyDate();
        }

        if (dateMil == preOnlyDate) {
            return true;
        } else {
            return dateMil > preOnlyDate && dateMil < currentOnlyDateMil;
        }
    }

    public static Calendar initOnlyDate() {
        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(ServerTime.getInstance(0).getServerRealTimeMilByEla());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
//        currentOnlyDate=calendar.getTime();
        currentOnlyDateMil = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        nextOnlyDate = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        preOnlyDate = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar;
    }

    public static String formatDate(java.util.Date date, String patter) {
        if (date == null || patter == null) {
            return null;
        }
        long curDateTime = System.currentTimeMillis();
        java.util.Date curDate = new Date(curDateTime);

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(curDate);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date);
        SimpleDateFormat format = null;
        if (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
            format = new SimpleDateFormat(PATTERN_IN_OLD, Locale.CHINA);
        } else {
            format = new SimpleDateFormat(PATTERN_IN_NEW, Locale.CHINA);
        }
        return format.format(date);
    }

    public static String formatDateNoYear(java.util.Date date, String patter) {
        if (date == null || patter == null) {
            return null;
        }
        long curDateTime = System.currentTimeMillis();
        java.util.Date curDate = new Date(curDateTime);

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(curDate);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date);
        SimpleDateFormat format = null;
        format = new SimpleDateFormat(PATTERN_IN_NEW, Locale.CHINA);

        return format.format(date);
    }

    /**
     * 返回发布时间距离当前的时间
     */
    public static String timeAgo(Date createdTime) {
        SimpleDateFormat format = new SimpleDateFormat(PATTERN_OUT_MONTH_TIME, Locale.CHINA);
        if (createdTime != null) {
            long agoTimeInMin = (new java.util.Date(System.currentTimeMillis()).getTime() - createdTime.getTime()) / 1000 / 60;
            //如果在当前时间以前一分钟内
//            if (agoTimeInMin <= 1) {
//                return "刚刚";
//            } else
            if (agoTimeInMin <= 60) {
                //如果传入的参数时间在当前时间以前10分钟之内
                return agoTimeInMin + "分钟前";
            } else if (agoTimeInMin <= 60 * 24) {
                return agoTimeInMin / 60 + "小时前";
//            } else if (agoTimeInMin <= 60 * 24 * 2) {
//                return agoTimeInMin / (60 * 24) + "天前";
            } else {
                return format.format(createdTime);
            }
        } else {
            return format.format(new Date(0));
        }
    }

    /**
     * 之后
     *
     * @param createdTime
     * @return
     */
    public static String timeLater(long createdTime) {
        if (createdTime > 0) {
            long agoTimeInMin = (createdTime - new java.util.Date(System.currentTimeMillis()).getTime()) / 1000;
            String txt = "";
            if (agoTimeInMin > 0) {
                //如果传入的参数时间在当前时间以前10分钟之内
                txt += agoTimeInMin / (24 * 60 * 60) + "天";
                long l = agoTimeInMin % (24 * 60 * 60);
                if (l > 0) {
                    txt += l / (60 * 60) + "时";
                    long m = l % (60 * 60);
                    if (m > 0) {
                        txt += m / 60 + "分";
                        long s = m % 60;
                        txt += s + "秒";

                    }
                }

                return txt;
            }
        }
        return "";
    }

    public static long paserStringDate(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        java.util.Date da = null;
        try {
            da = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return da == null ? 0 : da.getTime();
    }

    public static String getSixHourBeforeStr() {
//        long curentTime = ServerTime.getInstance(0).getServerRealTimeMilByEla();
        long curentTime = System.currentTimeMillis();
        long beforeSixTime = curentTime - sixBefore;
        long beforeSevenTime = curentTime - sevenBefore;
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_BEFORE_SIX);
        Log.e("getSixHourBeforeStr", "curDate:" + dateFormat.format(new java.util.Date(curentTime)));
        String startTime = dateFormat.format(new java.util.Date(beforeSevenTime));
        String endTime = dateFormat.format(new java.util.Date(beforeSixTime));
        return startTime + "-" + endTime;
    }

    public static long getCurrentOnlyDateMil() {
        return currentOnlyDateMil;
    }
}
